<template>
  <div class="loginBody">
    <div class="loginDiy">
      <div class="login-content">
        <h1 class="login-title">用户登录</h1>
        <el-form ref="loginForm" :model="loginForm"
                 :rules="rules" label-width="100px">
          <el-form-item label="账号" prop="no">
            <el-input v-model="loginForm.no" autocomplete="off" size="small"
                      style="margin-left: 5px" type="text">
            </el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" autocomplete="off" size="small"
                      style="margin-left: 5px" type="password" @keyup.enter.native="confirm">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button :disabled="confirm_disabled" type="primary" @click="confirm">确定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Login",
  data () {
    return {
      confirm_disabled: false,
      loginForm: {
        no: '',
        password: ''
      },
      rules: {
        no: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    confirm () {
      this.confirm_disabled = true;
      this.$refs.loginForm.validate ((valid) => {
        if (valid) { //valid成功为true,失败为false
          //去后台验证用户密码
          this.$axios.post (this.$HttpUrl + '/user/login', this.loginForm).then (res => res.data).then (res => {
            console.log (res)
            if (res.code === 200) {
              //存储
              sessionStorage.setItem ("CurUser", JSON.stringify (res.data.user))
              console.log (res.data.menu)
              this.$store.commit ("setMenu", res.data.menu)
              //跳转到主页
              this.$router.replace ('/Index-main');
            } else {
              this.confirm_disabled = false;
              alert ('用户名或密码错误!');
              return false;
            }
          });
        } else {
          this.confirm_disabled = false;
          console.log ('校验失败');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.loginBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #B3C0D1;
}

.loginDiy {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -200px;
  margin-left: -250px;
  width: 450px;
  height: 330px;
  background: #fff;
  border-radius: 5%;
}

.login-title {
  margin: 20px 0;
  text-align: center;
}

.login-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
</style>
